<?php
/**
* 微信登录
*/
class WeixinLogin_model extends MY_Model
{
	
	function __construct()
	{
		parent::__construct();
		//加载curl工具
		$this->load->model('Curl_model','curl');
    
		//微信参数
		$where = array(
          'id' => 1,
        );
    $info = $this->hw_getRecord('info', $where, '*');
    // $_SESSION['Weixin_biz'] = $info['biz'];
    // $_SESSION['Weixin_name'] = $info['name'];
    defined('APPID') or define('APPID', $info['appid']);
    defined('APPSECRET') or define('APPSECRET', $info['appsecret']);
	}
	/**
	 * 跳转获取code 方式为userinfo 不管是否关注都能获取用户基本信息
	 * @return [type] [description]
	 */
	public function header(){
    	$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=http%3A%2F%2F".$_SERVER['SERVER_NAME']."%2FWeixin%2FLogin%2FweixinLoginBack&response_type=code&scope=snsapi_userinfo&state=23edsd32defewfeefewf#wechat_redirect";
     //print_r($url);die();
      redirect($url);
    }

    public function hankHeader(){
      $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=http%3A%2F%2F".$_SERVER['SERVER_NAME']."%2FBanker%2FLogin%2FbankerLoginBack&response_type=code&scope=snsapi_userinfo&state=23edsd32defewfeefewf#wechat_redirect";
     //print_r($url);die();
      redirect($url);
    }


    /**
     * 跳转获取code 方式为userinfo 不管是否关注都能获取负责人基本信息
     * @return [type] [description]
     */
    public function headerLeader(){
        
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=http%3A%2F%2F".$_SERVER['SERVER_NAME']."%2FLeader%2FLogin%2FleaderLoginBack&response_type=code&scope=snsapi_userinfo&state=23edsd32defewfeefewf#wechat_redirect";
     //print_r($url);die();
      redirect($url);
    }


    function testheader(){
      $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=http%3A%2F%2F".$_SERVER['SERVER_NAME']."%2FWeiWeb%2FLogin%2FweixinLoginBack&response_type=code&scope=snsapi_userinfo&state=23edsd32defewfeefewf#wechat_redirect";
     
        redirect($url);
    }

     /**
     * 微信分享
     * @return [type] [description]
     */
     function headerfx(){
       $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".APPID."&redirect_uri=http%3A%2F%2F".$_SERVER['SERVER_NAME']."%2FWeixin%2FLogin%2FweixinShareLoginBack&response_type=code&scope=snsapi_base&state=23edsd32defewfeefewf#wechat_redirect";
      redirect($url);
    }
    /**
     * 通过header跳转获取的code拉取微信用户信息
     * @param  [type] $code [description]
     * @return [type]       [description]
     */
    function getUserByCode($code){
   		$data = $this->get_access_token($code);
      	if(isset($data['access_token'])){
        	//拉取用户信息
        	$user_res = $this->get_user_info($data);
        	return $user_res;
      	}
      	return false;
    }


     /**
      * @abstract 获取用户详细信息
    */
    function get_leader_code($code){
      $this->load->model('Token_model','token');
      $data = $this->get_access_token($code);
      $token = $this->token->getAccessToken();//微信票据的access_token
      $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid&lang=zh_CN";
      $user_info = json_decode(file_get_contents($url),true);
      return $user_info;  
    }

    /**
     * [已得到openid时获取用户信息]
     * @AuthorHTL
     * @DateTime  2017-04-10T18:09:49+0800
     * @param     [type] $openid    [用户唯一标示]
     * @return    [type] $user_info [用户信息]
     */
    function get_code($openid)
    {
      $this->load->model('Token_model','token');
      $token = $this->token->getAccessToken();//微信票据的access_token
      $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid&lang=zh_CN";
      $user_info = json_decode(file_get_contents($url),true);
      return $user_info;  
    }

    /**
     * 微信接口获取token
     * @return [type] [description]
     */
    //  function getToken(){
    //   $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.appid.'&secret='.appsecret;
    //   $token = $this->curl->_get($url);
    //   return $token['access_token'];
    // }

    /**
     * 获取抓取用户信息的access_token  不同于微信票据的access_token
     * @param  [type] $code [description]
     * @return [type]       [description]
     */
    function get_access_token($code){  
	    $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".APPID."&secret=".APPSECRET."&code=".$code."&grant_type=authorization_code";  

	    $data = $this->curl->_get($url);   
	    if(isset($data['access_token'])){
	        return $data;
	    }else{  
	        // return "获取access_token错误";  
	        return false;
	    }         
    }
    /**
     * @abstract 拉取微信用户信息
     */
    function get_user_info($res){
	    $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$res['access_token']."&openid=".$res['openid']."&lang=zh_CN";  
	      
	    $data = $this->curl->_get($url);//这个地方不能用file_get_contents   
	      
	    if($data['openid']){
	        return $data;
	    }else{
	        return false;
	    }
    }
}